모듈성의 구조
재사용성은 취약한 선형적 순서에서 강력한 교환 가능한 구성 요소 시스템으로 소프트웨어를 전환합니다. 논리를 독립적인 함수로 추상화함으로써 우리는 단일 진리 원칙을 확립하게 됩니다. 이는 화성 탐사선의 환경 감시 시스템(REMS)과 같은 시스템에서 매우 중요합니다. 이 시스템은 코드 중복 없이 다각도의 데이터 스트림을 처리해야 합니다.
함수가 중요한 이유
코드를 함수로 나누는 것은 이해하고, 재사용하며, 유지보수하기 더 쉬워집니다. 이는 DRY(Don't Repeat Yourself, 반복하지 마세요) 원칙을 따릅니다: 센서의 원시 전압을 섭씨로 변환하는 로직은 한 번만 정의하고 어디서든 호출되어야 하며, '복사-붙여넣기' 버그를 방지합니다.
빠른 점검 12.2
코드를 함수로 나누는 것의 장점은 무엇인가요? 모듈성은 개발자가 하나의 논리적 단위에 집중할 수 있게 해주며 디버깅과 테스트를 간편하게 만듭니다.
main.py
TERMINALbash — 80x24
> Ready. Click "Run" to execute.
>
QUESTION 1
What are some advantages of splitting code into functions?
It makes the code execute faster by the Go compiler.
It makes the code easier to understand, reuse, and maintain.
It eliminates the need for variable declarations.
It allows functions to run without being called.
✅ Correct!
Correct! Modularity reduces cognitive load and centralizes logic.❌ Incorrect
The primary benefits are architectural: readability, reusability, and maintenance.QUESTION 2
Do you call a function with arguments or parameters?
Parameters
Arguments
Identifiers
Receivers
✅ Correct!
Correct! You pass 'arguments' (the actual values) to a function that has defined 'parameters' (the placeholders).❌ Incorrect
Think of it this way: Parameters are the definitions in the declaration; Arguments are the values used during the call.QUESTION 3
If there existed a groundSensor function that returned a celsius temperature, could it be assigned to a variable of a function type requiring the same signature?
Yes, if the return types and parameters match.
No, because function names must be unique.
Only if the function is exported (Uppercase).
No, functions cannot be assigned to variables in Go.
✅ Correct!
Exactly. In Go, functions are first-class citizens and can be assigned to variables if their signatures match.❌ Incorrect
Go allows functions to be treated as values, provided the signature compatibility is maintained.QUESTION 4
What does the DRY principle stand for?
Data Retrieval Yield
Don't Repeat Yourself
Define Regular Yields
Distributed Runtime Yield
✅ Correct!
DRY ensures that a specific logic is defined once, preventing logic drift and bugs.❌ Incorrect
DRY is about avoiding duplication: Don't Repeat Yourself.QUESTION 5
How does modularity help in managing complex systems like the Mars Rover?
It allows every sensor to use the same physical memory space.
It allows independent logic for different data streams like wind and pressure.
It prevents the rover from needing an operating system.
It makes the hardware lighter.
✅ Correct!
Yes! Each sensor module can be handled by a specific function, keeping the system organized.❌ Incorrect
Modularity is a software design pattern to manage complexity, not a hardware weight reduction method.Case Study: The REMS Data Pipeline
Applying Modularity to Mars Exploration
The Mars Rover REMS needs to gather atmospheric pressure and ground temperature. Currently, the code is a single 500-line main function. You are tasked with refactoring this into modular components.
Q
1. Why would creating a separate 'convertVoltageToCelsius' function be better than doing the math directly inside the main loop?
Solution:
It centralizes the conversion logic. If the sensor calibration changes, you only update one function instead of searching through the entire main loop for every instance of the math.
It centralizes the conversion logic. If the sensor calibration changes, you only update one function instead of searching through the entire main loop for every instance of the math.
Q
2. If we add a third sensor for UV radiation, how does our modular approach simplify this addition?
Solution:
We simply define a new function for the UV sensor that follows the established data format and call it within our existing data collector logic, rather than rewriting the collection framework.
We simply define a new function for the UV sensor that follows the established data format and call it within our existing data collector logic, rather than rewriting the collection framework.